//
//  XYGPUImageController.m
//  XYSfxyxuSwift
//
//  Created by 许洋 on 2018/4/12.
//  Copyright © 2018年 许洋. All rights reserved.
//

#import "XYGPUImageController.h"
#import <GPUImage/GPUImage.h>
#import "XYCollectionView.h"
#import "XYGPUFilterModel.h"
//#import <>
@interface XYGPUImageController ()<XYCollectionViewDelegate>
@property (nonatomic,strong) UIImageView *imageView;

@property (nonatomic,strong) GPUImageView *filterView;

@property (nonatomic,strong) GPUImageVideoCamera *videoCamera;

@property (nonatomic,strong) XYCollectionView *collectionView;




@end

@implementation XYGPUImageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self.navigationController setNavigationBarHidden:YES];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"GPUFilter.json" ofType:nil];
    
    //加载JSON文件
    NSData *data = [NSData dataWithContentsOfFile:path];
    //将JSON数据转为NSArray或NSDictionary
    NSError *error = nil;
    NSArray *dictArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    
    _collectionView = [[XYCollectionView alloc]initWithFrame:CGRectMake(20, XYScreenHeight -64 - 50 - 34, XYScreenWidth - 40, 50)];
    _collectionView.dataSource = [XYGPUFilterModel mj_objectArrayWithKeyValuesArray:dictArray];
    _collectionView.itemSize = CGSizeMake(100, 50);
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuide).offset(-10);
        make.height.mas_equalTo(50);
    }];
    self.videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionFront];
    self.videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    self.videoCamera.horizontallyMirrorFrontFacingCamera = YES;
    
    self.filterView = [[GPUImageView alloc] init];
    self.filterView.center = self.view.center;
    [self.view addSubview:self.filterView];
    [self.filterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(_collectionView.mas_top).offset(-10);
    }];
    [self.videoCamera addTarget:self.filterView];
    [self.videoCamera startCameraCapture];
    
}
-(UICollectionViewCell *)xy_collectionView:(XYCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
     UICollectionViewCell * cell = [collectionView xy_dequeueReusableCellWithReuseIdentifier:@"XYColorCollectionViewCell" forIndexPath:indexPath];
   
    return cell;
}
-(void)xy_collectionView:(XYCollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    XYGPUFilterModel *model = collectionView.dataSource[indexPath.row];
    if (model.className.length > 0) {
        [self.videoCamera removeAllTargets];
        Class class = NSClassFromString(model.className);
        GPUImageFilter *filter = (GPUImageFilter *)[[class alloc] init];
        [self.videoCamera addTarget:filter];
        [filter addTarget:self.filterView];
    }else{
        [self.videoCamera removeAllTargets];
        [self.videoCamera addTarget:self.filterView];
    }
   
}
- (void)clickAction:(UIButton *)btn{
    if (btn.selected) {
//        btn.selected = NO;
        [self.videoCamera removeAllTargets];
        [self.videoCamera addTarget:self.filterView];
    } else {
//        btn.selected = YES;
        [self.videoCamera removeAllTargets];
        GPUImageBilateralFilter *beautifyFilter = [[GPUImageBilateralFilter alloc] init];
        [self.videoCamera addTarget:beautifyFilter];
        [beautifyFilter addTarget:self.filterView];
    }
    btn.selected = !btn.selected;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
